package com.jiyun.service;

import org.elasticsearch.action.delete.DeleteRequest;
import org.elasticsearch.action.delete.DeleteResponse;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.action.support.WriteRequest;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.index.query.*;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.elasticsearch.search.sort.SortOrder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
public class UserService {
    @Autowired
    RestHighLevelClient restHighLevelClient;

    public List<Map> findAll(Map map) throws IOException {
        //创建请求对象
        SearchRequest searchRequest = new SearchRequest("user");
        String name = map.get("name").toString();
        String age1 = map.get("age1").toString();
        String age2 = map.get("age2").toString();
        //需要连接的表格
        searchRequest.types("doc");
        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
        BoolQueryBuilder boolQueryBuilder = new BoolQueryBuilder();
        if (name.equals("")) {
            QueryBuilder queryBuilder = QueryBuilders.matchAllQuery();
            boolQueryBuilder.must(queryBuilder);
        } else {
            MatchQueryBuilder matchQueryBuilder = QueryBuilders.matchQuery("name", name);
            boolQueryBuilder.must(matchQueryBuilder);
        }
        if (!age1.equals("")) {
            RangeQueryBuilder rangeQueryBuilder = QueryBuilders.rangeQuery("age").gte(age1);
            boolQueryBuilder.filter(rangeQueryBuilder);
        }
        if (!age2.equals("")) {
            RangeQueryBuilder rangeQueryBuilder = QueryBuilders.rangeQuery("age").lte(age2);
            boolQueryBuilder.filter(rangeQueryBuilder);
        }
        searchSourceBuilder.query(boolQueryBuilder);
        //排序
        searchSourceBuilder.sort("id", SortOrder.ASC);
        searchRequest.source(searchSourceBuilder);
        //接受响应
        SearchResponse searchResponse = restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT);
        SearchHits hits = searchResponse.getHits();
        SearchHit[] hits1 = hits.getHits();
        List<Map> list = new ArrayList<>();
        for (SearchHit documentFields : hits1) {
            Map<String, Object> sourceAsMap = documentFields.getSourceAsMap();
            list.add(sourceAsMap);
        }
        return list;
    }

    public Integer add(Map map) throws IOException {
        String id = map.get("id").toString();
        IndexRequest indexRequest = new IndexRequest("user", "doc", id);
        indexRequest.setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE);
        indexRequest.source(map);
        IndexResponse indexResponse = restHighLevelClient.index(indexRequest, RequestOptions.DEFAULT);
        return indexResponse.status().getStatus();

    }

    public Integer del(Integer id) throws IOException {
        DeleteRequest deleteRequest = new DeleteRequest("user", "doc", id + "");
        deleteRequest.setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE);
        DeleteResponse deleteResponse = restHighLevelClient.delete(deleteRequest, RequestOptions.DEFAULT);
        return deleteResponse.status().getStatus();
    }
}
